package com.clouds.wms.cms;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.clouds.wms.bean.Cmsmj;
import com.clouds.wms.bean.Cmsmd;
import com.clouds.wms.bean.Cmsmk;
import com.clouds.wms.exception.SysException;
import com.clouds.wms.service.CmsmjService;
import com.clouds.wms.service.CmsmkService;
import com.clouds.wms.util.Function;
import com.clouds.wms.util.PagesBean;
import com.clouds.wms.util.RequestHelper;
import com.clouds.wms.util.SysParams;

@Controller
@RequestMapping("/cmsmj")
public class CmsmjController {

	private Log log = LogFactory.getLog(getClass());
	
	@Resource(name="cmsmjService")
	private CmsmjService cmsmjService;

	@Resource(name="cmsmkService")
	private CmsmkService cmsmkService;

	/**
	 * 单据类别管理
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 */
	@RequestMapping(value = "/cmsmjMain.xhtml")
	public String cmsmjMain(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		return "cms/cmsmj";
	}

	/**
	 * 列出所有的单据类别
	 * @param request
	 * @param response
	 * @return
	 * @throws SysException
	 */
	@RequestMapping(value = "/cmsmjList.xhtml")
	public String cmsmjList(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String mjaa = RequestHelper.getString(request, "mjaa");
		String mjab = RequestHelper.getString(request, "mjab");
		int mjac = RequestHelper.getInt(request, "mjac", 0);
		int mjae = RequestHelper.getInt(request, "mjae", 0);
		int pageNumber = RequestHelper.getInt(request, "page", 1);// 要跳转的页号
		int pageSize = RequestHelper.getInt(request, "rows", 10);// 一页的条数
		PagesBean pb = cmsmjService.getCmsmjList(mjaa, mjab, mjac, mjae, pageNumber, pageSize);
		List<Cmsmj> list = ((List<Cmsmj>)pb.getData());
		int count = pb.getCount();
		Map<String,Object> paraMap = new HashMap<String,Object>();
		paraMap.put("total", count);
		paraMap.put("rows", list);
		try {
			Function.toJson(response, paraMap);
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		return null;
	}

	/**
	 * 添加单据类别
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/saveCmsmj.xhtml", method = RequestMethod.POST)
	public String saveCmsmj(HttpServletRequest request, HttpServletResponse response, Model model, Cmsmj cmsmj) throws SysException, IOException {
		String mjaa = cmsmj.getMjaa();
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if(mjaa == null || "".equals(mjaa.trim())) {
			out.print("单据类别编号不能为空!");
			return null;
		}
		Cmsmj cmsmj1 = cmsmjService.getCmsmjByMjaa(mjaa);
		if(cmsmj1 != null && cmsmj1.getMjaa() != null && !"".equals(cmsmj1.getMjaa())) {
			out.print("单据类别编号已经存在!");
			return null;
		}
		cmsmj.setMjae(SysParams.STATUS_NORMAL);
		cmsmjService.saveCmsmj(cmsmj);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("增加了编号为[" + mjaa + "]的单据类别!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}

	/**
	 * 修改单据类别
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/updateCmsmj.xhtml", method = RequestMethod.POST)
	public String updateCmsmj(HttpServletRequest request, HttpServletResponse response, Model model, Cmsmj cmsmj) throws SysException, IOException {
		String mjaa = cmsmj.getMjaa();
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if(mjaa == null || "".equals(mjaa.trim())) {
			out.print("单据类别编号不能为空!");
			return null;
		}
		Cmsmj cmsmj1 = cmsmjService.getCmsmjByMjaa(mjaa);
		if(cmsmj1 != null && cmsmj1.getMjaa() != null && !"".equals(cmsmj1.getMjaa()) && !cmsmj1.getMjaa().equals(cmsmj.getMjaa())) {
			out.print("单据类别编号已经存在!");
			return null;
		}
		cmsmjService.updateCmsmj(cmsmj);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("修改了编号为[" + mjaa + "]的单据类别!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}

	/**
	 * 删除单据类别
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/deleteCmsmj.xhtml", method = RequestMethod.POST)
	public String deleteCmsmj(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mjaa = RequestHelper.getString(request, "mjaa");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		cmsmjService.deleteCmsmjByMjaa(mjaa);
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("删除了编号为[" + mjaa + "]的单据类别!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}

	/**
	 * 批量删除单据类别
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/deleteCmsmjBatch.xhtml", method = RequestMethod.POST)
	public String deleteCmsmjBatch(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mjaas = RequestHelper.getString(request, "mjaa");
		String[] mjaaStrs = mjaas.split(",");
		for(String mjaa : mjaaStrs) {
			cmsmjService.deleteCmsmjByMjaa(mjaa);
			Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
			Cmsmk cmsmk = new Cmsmk();
			cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
			cmsmk.setMkac(globalUmUser.getMdac());
			cmsmk.setMkad("删除了编号为[" + mjaa + "]的单据类别!");
			cmsmk.setMkae(new Date());
			cmsmk.setMkaf(request.getRemoteAddr());
			cmsmkService.saveCmsmk(cmsmk);
		}
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print("success");
		return null;
	}
	
	/**
	 * 禁用单据类别
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/disableCmsmj.xhtml", method = RequestMethod.POST)
	public String disableCmsmj(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mjaa = RequestHelper.getString(request, "mjaa");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		cmsmjService.updateCmsmjStatus(mjaa, SysParams.STATUS_DISABLE);
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("禁用了编号为[" + mjaa + "]的单据类别!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}

	/**
	 * 批量禁用单据类别
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/disableCmsmjBatch.xhtml", method = RequestMethod.POST)
	public String disableCmsmjBatch(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mjaas = RequestHelper.getString(request, "mjaa");
		String[] mjaaStrs = mjaas.split(",");
		for(String mjaa : mjaaStrs) {
			cmsmjService.updateCmsmjStatus(mjaa, SysParams.STATUS_DISABLE);
			Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
			Cmsmk cmsmk = new Cmsmk();
			cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
			cmsmk.setMkac(globalUmUser.getMdac());
			cmsmk.setMkad("禁用了编号为[" + mjaa + "]的单据类别!");
			cmsmk.setMkae(new Date());
			cmsmk.setMkaf(request.getRemoteAddr());
			cmsmkService.saveCmsmk(cmsmk);
		}
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print("success");
		return null;
	}

	/**
	 * 启用单据类别
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/startUsingCmsmj.xhtml", method = RequestMethod.POST)
	public String startUsingCmsmj(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mjaa = RequestHelper.getString(request, "mjaa");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		cmsmjService.updateCmsmjStatus(mjaa, SysParams.STATUS_NORMAL);
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("启用了编号为[" + mjaa + "]的单据类别!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}

	/**
	 * 批量启用单据类别
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/startUsingCmsmjBatch.xhtml", method = RequestMethod.POST)
	public String startUsingCmsmjBatch(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mjaas = RequestHelper.getString(request, "mjaa");
		String[] mjaaStrs = mjaas.split(",");
		for(String mjaa : mjaaStrs) {
			cmsmjService.updateCmsmjStatus(mjaa, SysParams.STATUS_NORMAL);
			Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
			Cmsmk cmsmk = new Cmsmk();
			cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
			cmsmk.setMkac(globalUmUser.getMdac());
			cmsmk.setMkad("启用了编号为[" + mjaa + "]的单据类别!");
			cmsmk.setMkae(new Date());
			cmsmk.setMkaf(request.getRemoteAddr());
			cmsmkService.saveCmsmk(cmsmk);
		}
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.print("success");
		return null;
	}

}
